import 'package:xim/http/apis/message/message_model.dart';
import 'package:xim/http/apis/session/session_model.dart';
import 'package:xim/http/apis/user/user_model.dart';
import 'package:xim/http/http.dart';

/// 会话相关API
class SessionApi {
  /// 获取会话成员列表
  static Future<List<UserInfoModel>> querySessionMembers(int sessionId) async {
    var respData = await dio
        .postResult("/store/session/member/query", queryParameters: {"sessionId": sessionId});
    var membersInfo = respData.data as List;
    return membersInfo.map((e) => UserInfoModel.fromJson(e)).toList();
  }

  /// 退出群聊
  static Future<ResponseResult> requestQuitSession(int sessionId) async {
    return await dio.postResult("/store/session/quit", queryParameters: {"sessionId": sessionId});
  }

  /// 获取会话列表
  static Future<List<SessionMessageModel>> requestSessionList() async {
    var result = await dio.postResult("/store/session/list");
    var listData = result.data as List<dynamic>;
    return listData.map((e) {
      return SessionMessageModel(
        id: e["sessionId"],
        name: e["sessionName"],
        creatorAccount: e['creatorAccount'],
        createTime: DateTime.parse(e['createTime']),
        avatar: e['avatar'],
        unReadCount: e['unReadCount'],
        latestMessage:e['latestMessage']!=null
            ? ChatMessageModel.fromJson(e['latestMessage'])
            : null,
      );
    }).toList();
  }

  /// 获取会话信息
  static Future<SessionModel> querySessionInfoById(int sessionId) async {
    var result = await dio.postResult(
        "/store/session/query",
        queryParameters: {"sessionId": sessionId}
    );
    return SessionModel.fromJson(result.data);
  }

  /// 创建会话
  static Future<int> requestCreateSession(List<Map<String, Object>> data) async {
    var respData = await dio.postResult("/store/session/create", data: data);
    return respData.data as int;
  }

}
